﻿using BLL;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;

namespace WindowsForms
{
    public partial class FrmClubManage : Form
    {
        public FrmClubManage()
        {
            InitializeComponent();
            #region   初始化控件缩放

            x = Width;
            y = Height;
            setTag(this);

            #endregion
        }



        private void lstClubList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstClubList.SelectedIndex != -1)
            {
                Club cm = (Club)lstClubList.SelectedItem;
                txtClubName.Text = cm.Clubname;
                txtTeacher.Text = cm.Teachername;
                dtpFounddate.Value = cm.Founddate;
                txtIntroduction.Text = cm.Introduction;
            }
        }

        private void FrmClubManage_Load(object sender, EventArgs e)
        {
            ClubManage clubbll = new ClubManage();
            dgvClub.AutoGenerateColumns = false;
            string sql = "select * from club";
            dgvClub.DataSource = clubbll.GetALLClub(sql);


            //将类库Model中Club类（社团）实例化对象
            //添加ListBox列表项
            Club club1 = new Club(1, "爱心社", Convert.ToDateTime("2001-4-8"), "张小林", "扶贫助困，爱心助学");
            Club club2 = new Club(2, "摄影协会", Convert.ToDateTime("2008-10-8"), "王文泉", "发现美，记录美");
            Club club3 = new Club(3, "嘻哈街舞团", Convert.ToDateTime("2010-9-1"), "周雪君", "舞动青春，扬我风采");
            Club club4 = new Club(4, "望月文学社", Convert.ToDateTime("2012-9-1"), "陶育英", "深耕文学，发展艺术");
            Club club5 = new Club(5, "英语协会", Convert.ToDateTime("2010-5-5"), "李雪茹", "寓学于乐，快乐学英语");

            //将Club类初始化社团信息添加到列表框lstClubList中
            lstClubList.Items.Add(club1);
            lstClubList.Items.Add(club2);
            lstClubList.Items.Add(club3);
            lstClubList.Items.Add(club4);
            lstClubList.Items.Add(club5);
            lstClubList.DisplayMember = "Clubname";//列表框只显示社团名称


            Club cm = (Club)dgvClub.CurrentRow.DataBoundItem;
            LoadDetail(cm);
        }

        private void LoadDetail(Club cm)
        {
            
            txtClubName.Text = cm.Clubname;
            txtTeacher.Text = cm.Teachername;
            dtpFounddate.Value = cm.Founddate;
            txtIntroduction.Text = cm.Introduction;
        }

        private void dgvClub_CellClick(object sender, DataGridViewCellEventArgs e)
        {

            Club cm = (Club)dgvClub.CurrentRow.DataBoundItem;
            LoadDetail(cm);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            ActivityManage memberbll = new ActivityManage();
            string sql = "SELECT * FROM activity,club where activity.clubid=club.clubid";
            memberbll.GetALLActivities(sql);
            int clubid = 2;
            string clubname = txtClubName.Text;
            DateTime founddate = dtpFounddate.Value;
            string introduction = txtIntroduction.Text;
            string teachername = txtTeacher.Text;

            Club cm = new Club(clubid, clubname, founddate, introduction, teachername);
            ClubManage clubbll = new ClubManage();

            if (clubbll.AddClub(cm))
                MessageBox.Show("社团保存成功!");
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            ActivityManage memberbll = new ActivityManage();
            string sql = "SELECT * FROM activity,club where activity.clubid=club.clubid";
            memberbll.GetALLActivities(sql);
            int clubid = int.Parse(memberbll.GetALLActivities(sql).Count.ToString()) + 1;
            string clubname = txtClubName.Text;
            DateTime founddate = dtpFounddate.Value;
            string introduction = txtIntroduction.Text;
            string teachername = txtTeacher.Text;
            
            Club cm = new Club(clubid, clubname, founddate, introduction, teachername);
            lstClubList.Items.Add(cm);

            ClubManage clubbll = new ClubManage();
            

            if (clubbll.AddClub(cm))
                MessageBox.Show("社团添加成功!");
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (lstClubList.SelectedIndex != -1)
            {
                lstClubList.Items.Remove(lstClubList.SelectedItem);
                lstClubList.SelectedIndex = 0;
            }

            ActivityManage memberbll = new ActivityManage();
            string sql = "SELECT * FROM activity,club where activity.clubid=club.clubid";
            memberbll.GetALLActivities(sql);
            int clubid = int.Parse(memberbll.GetALLActivities(sql).Count.ToString()) + 2;
            string clubname = txtClubName.Text;
            DateTime founddate = dtpFounddate.Value;
            string introduction = txtIntroduction.Text;
            string teachername = txtTeacher.Text;

            Club cm = new Club(clubid, clubname, founddate, introduction, teachername);
            

            ClubManage clubbll = new ClubManage();


            if (clubbll.DelClub(cm))
                MessageBox.Show("社团删除成功!");
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            ActivityManage memberbll = new ActivityManage();
            string sql = "SELECT * FROM activity,club where activity.clubid=club.clubid";
            memberbll.GetALLActivities(sql);
            int clubid = int.Parse(memberbll.GetALLActivities(sql).Count.ToString()) + 2;


            string clubname = txtClubName.Text;
            DateTime founddate = dtpFounddate.Value;
            string introduction = txtIntroduction.Text;
            string teachername = txtTeacher.Text;

            Club cm = new Club(clubid, clubname, founddate, introduction, teachername);
           
            ClubManage clubbll = new ClubManage();

            if (clubbll.UptClub(cm))
                MessageBox.Show("社团修改成功!");
        }

        private void FrmClubManage_Resize(object sender, EventArgs e)
        {
            //重置窗口布局
            ReWinformLayout();
        }

        #region 控件大小随窗体大小等比例缩放

        private readonly float x; //定义当前窗体的宽度
        private readonly float y; //定义当前窗体的高度

        private void setTag(Control cons)
        {
            foreach (Control con in cons.Controls)
            {
                con.Tag = con.Width + ";" + con.Height + ";" + con.Left + ";" + con.Top + ";" + con.Font.Size;
                if (con.Controls.Count > 0) setTag(con);
            }
        }

        private void setControls(float newx, float newy, Control cons)
        {
            //遍历窗体中的控件，重新设置控件的值
            foreach (Control con in cons.Controls)
                //获取控件的Tag属性值，并分割后存储字符串数组
                if (con.Tag != null)
                {
                    var mytag = con.Tag.ToString().Split(';');
                    //根据窗体缩放的比例确定控件的值
                    con.Width = Convert.ToInt32(Convert.ToSingle(mytag[0]) * newx); //宽度
                    con.Height = Convert.ToInt32(Convert.ToSingle(mytag[1]) * newy); //高度
                    con.Left = Convert.ToInt32(Convert.ToSingle(mytag[2]) * newx); //左边距
                    con.Top = Convert.ToInt32(Convert.ToSingle(mytag[3]) * newy); //顶边距
                    var currentSize = Convert.ToSingle(mytag[4]) * newy; //字体大小                   
                    if (currentSize > 0) con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
                    con.Focus();
                    if (con.Controls.Count > 0) setControls(newx, newy, con);
                }
        }


        /// <summary>
        /// 重置窗体布局
        /// </summary>
        private void ReWinformLayout()
        {
            var newx = Width / x;
            var newy = Height / y;
            setControls(newx, newy, this);

        }

        #endregion
    }
}

